package com.example.football.service;


import com.example.football.dao.PeoplelikestarDao;
import com.example.football.dao.PeopleliketeamDao;
import com.example.football.mapper.PeoplelikestarMapper;
import com.example.football.mapper.PeopleliketeamMapper;
import com.example.football.pojo.Peoplelikestar;
import com.example.football.pojo.Peopleliketeam;
import com.example.football.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class PeoplelikestarServiceImpl implements PeoplelikestarService{
    @Resource
    private PeoplelikestarDao sd;

    @Override
    public List<Peoplelikestar> queryAll() {

        return sd.selectAll();
    }

    @Override
    public void removeByPeoplelikestarid(Integer id) {
        try {
            sd.deleteByPeoplelikestarid(id);
        } catch (Exception e) {
            throw new RuntimeException("根据ID删除有异常");
        }
    }

    @Override
    public List<Peoplelikestar> queryByLikestar(String likestar) {

        return sd.selectByLikestar(likestar);
    }

    @Override
    public Peoplelikestar queryById(Integer id) {

        return sd.selectById(id);
    }

    @Override
    public void addPeoplelikestar(Peoplelikestar peoplelikestar) {
        try {
            sd.insertPeoplelikestar(peoplelikestar);
        } catch (Exception e) {
            throw new RuntimeException("添加有异常");
        }
    }

    @Override
    public void changePeoplelikestar(Peoplelikestar peoplelikestar) {
        try {
            sd.updatePeoplelikestar(peoplelikestar);
        } catch (Exception e) {
            throw new RuntimeException("修改数据异常");
        }
    }

    @Autowired
    private PeoplelikestarMapper userMapper;

    public Result insert(Peoplelikestar peoplelikestar) {
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            Peoplelikestar userByName = userMapper.findUserByLikestar(peoplelikestar.getPeopleid(),peoplelikestar.getLikestar());
            if(userByName!=null){
                //如果查询到 说明存在
                result.setMsg("已添加过");
            }else {
                userMapper.insert(peoplelikestar);
                result.setMsg("添加成功");
                result.setSuccess(true);
                result.setDetail(peoplelikestar);
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
